<template>
  <div>
    <div class="upload">
      <div class="uploadBox">
        <a-upload
          accept=".xls,.xlsx,.csv,.xlsm"
          name="file"
          :data="{type:2}"
          :headers="headers"
          :action="url+'/common/sysFile/upload'"
          :showUploadList="false"
          ref="uploadRef"
          @change="handleChange"
        >
          <a-button type="primary" :icon="h(CloudUploadOutlined)">上传文件</a-button>
          <a-button type="primary" :icon="h(DownloadOutlined)" @click="clickDownLoad">下载</a-button>
        </a-upload>
      </div>

      <div class="msgBox" v-if="fileName">
        <p>{{fileName}}</p>
        <p>{{fileTime}}</p>
      </div>

<!--      <div class="msgBox" v-if="fileName">-->
<!--        <p>下载每日更新文件</p>-->
<!--        <p>{{fileName}}</p>-->
<!--        <p>{{fileTime}}</p>-->
<!--        <a-button type="primary" :icon="h(DownloadOutlined)" @click="clickDownLoadHistory">下载</a-button>-->
<!--      </div>-->
    </div>
    <WebsiteInfoModal ref="registerModal" @success="handleSuccess"></WebsiteInfoModal>
  </div>
</template>

<script lang="ts" name="novel-websiteInfo" setup>
  import { ref, reactive,h } from 'vue';
  import { BasicTable, useTable, TableAction } from '/@/components/Table';
  import { useListPage } from '/@/hooks/system/useListPage';
  import { columns } from './WebsiteInfo.data';
  import { list, deleteOne, batchDelete, getImportUrl, getExportUrl } from './WebsiteInfo.api';
  import { downloadFile } from '/@/utils/common/renderUtils';
  import WebsiteInfoModal from './components/WebsiteInfoModal.vue'
  import { defHttp } from '/@/utils/http/axios';
  import { message } from 'ant-design-vue';
  import { DownloadOutlined,CloudUploadOutlined} from '@ant-design/icons-vue';
  import { getFileAccessHttpUrl, getHeaders } from '/@/utils/common/compUtils';

  import {useMessage} from "@/hooks/web/useMessage";
  import moment from 'moment';
  import {getFileblob} from "@/api/common/api";
  import { useGlobSetting } from '/@/hooks/setting';
  const globSetting = useGlobSetting();
  const { createMessage, createErrorModal } = useMessage();
  const formRef = ref();

  let queryParam = reactive<any>({

  });
  const baseUploadUrl = globSetting.uploadUrl;

  const url=ref(globSetting.uploadUrl)
  const headers = getHeaders();
  const spinning=ref<boolean>(true)
  const toggleSearchStatus = ref<boolean>(false);
  const registerModal = ref();
  //注册table数据
  const { prefixCls, tableContext, onExportXls, onImportXls } = useListPage({
    tableProps: {
      title: 'website_info',
      api: list,
      columns,
      canResize:false,
      useSearchForm: false,
      actionColumn: {
        width: 120,
        fixed: 'right',
      },
      beforeFetch: (params) => {
        return Object.assign(params, queryParam);
      },
    },
    exportConfig: {
      name: "website_info",
      url: getExportUrl,
      params: queryParam,
    },
	  importConfig: {
	    url: getImportUrl,
	    success: handleSuccess
	  },
  });
  const [registerTable, { reload, collapseAll, updateTableDataRecord, findTableDataRecord, getDataSource }, { rowSelection, selectedRowKeys }] = tableContext;
  const labelCol = reactive({
    xs: { span: 24 },
    sm: { span: 7 },
  });
  const wrapperCol = reactive({
    xs: { span: 24 },
    sm: { span: 16 },
  });

  const fileName=ref()
  const fileTime=ref()

  async function init() {
    const data=await defHttp.get({ url: '/common/sysFile/list'});
    console.log(data)
    fileName.value=data[0].url
    fileTime.value=data[0].createDate
  }

  init()

  /**
   * 文件上传结果回调
   */
  function handleChange({ file, fileList, event }) {
    if (file.response){
      if (file.response.success){
        fileName.value=file.response.result
        fileTime.value = moment(file.response.timestamp).format('YYYY-MM-DD HH:mm:SS');
        createMessage.success('上传成功！')
      }else {
        createMessage.error(file.response.message)
      }
    }
  }

  /**
   * 点击下载
   */
  async function clickDownLoad() {
    window.location.href = url.value + `/sys/common/static/${fileName.value}`
  }

  /**
   * 点击下载历史
   */
  async function clickDownLoadHistory() {
    registerModal.value.disableSubmit = false;
    registerModal.value.add();
  }

  /**
   * 新增事件
   */
  function handleAdd() {
    registerModal.value.disableSubmit = false;
    registerModal.value.add();
  }

  /**
   * 编辑事件
   */
  function handleEdit(record: Recordable) {
    registerModal.value.disableSubmit = false;
    registerModal.value.edit(record);
  }

  /**
   * 详情
   */
  function handleDetail(record: Recordable) {
    registerModal.value.disableSubmit = true;
    registerModal.value.edit(record);
  }

  /**
   * 删除事件
   */
  async function handleDelete(record) {
    await deleteOne({ id: record.id }, handleSuccess);
  }

  /**
   * 批量删除事件
   */
  async function batchHandleDelete() {
    await batchDelete({ ids: selectedRowKeys.value }, handleSuccess);
  }

  /**
   * 成功回调
   */
  function handleSuccess() {
    (selectedRowKeys.value = []) && reload();
  }

  /**
   * 操作栏
   */
  function getTableAction(record) {
    return [
      {
        label: '编辑',
        onClick: handleEdit.bind(null, record),
      },
    ];
  }

  /**
   * 下拉操作栏
   */
  function getDropDownAction(record) {
    return [
      {
        label: '详情',
        onClick: handleDetail.bind(null, record),
      }, {
        label: '删除',
        popConfirm: {
          title: '是否确认删除',
          confirm: handleDelete.bind(null, record),
        }
      }
    ]
  }

  /**
   * 查询
   */
  function searchQuery() {
    reload();
  }

  /**
   * 重置
   */
  function searchReset() {
    formRef.value.resetFields();
    selectedRowKeys.value = [];
    //刷新数据
    reload();
  }


  async function clickSubmit() {
    let url=queryParam.id===''?'/novel/websiteInfo/add':'/novel/websiteInfo/edit'
    const data=await defHttp.post({ url, params:queryParam })
    message.success('操作成功');
  }




</script>

<style lang="less" scoped>
  .jeecg-basic-table-form-container {
    .table-page-search-submitButtons {
      display: block;
      margin-bottom: 24px;
      white-space: nowrap;
    }
    .query-group-cust{
      width: calc(50% - 15px);
      min-width: 100px !important;
    }
    .query-group-split-cust{
      width: 30px;
      display: inline-block;
      text-align: center
    }
  }

  .upload{
    display: flex;
  }

  .uploadBox{
    padding: 22px 0;
    background: white;
    display: flex;
    width: fit-content;
    align-items: center;
    margin: 26px;
    .ant-btn{
      margin: 0 22px;
    }
  }

  .msgBox{
    padding: 22px;
    background: white;
    margin: 26px;
  }
</style>
